from sqlalchemy import Column, Integer, String, DateTime, Date, ForeignKey
from database.database import Base
from datetime import datetime, date
from sqlalchemy.orm import relationship

class UserStats(Base):
    __tablename__ = "user_stats"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    stat_date = Column(Date, nullable=False, default=date.today)
    tts_count = Column(Integer, default=0)  # TTS文字数量
    asr_seconds = Column(Integer, default=0)  # ASR音频时长（秒）
    created_at = Column(DateTime, default=datetime.now)  # 使用服务器本地时间
    
    # 建立与用户表的关系
    user = relationship("User", back_populates="stats")
    
    # 确保每个用户每天只有一条统计记录
    __table_args__ = (
        {"comment": "用户每日TTS/ASR使用统计"}
    ) 